using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RecoFace
{
    using AForge.Video;
    using AForge.Video.DirectShow;

    public partial class FrmMain : Form
    {
        private FaceDetection face = new FaceDetection();
        private Camera cam = new Camera();
        Bitmap current;

        private int statIndex = 0; // current statistics index
        private int statReady = 0; // ready statistics values

        public FrmMain()
        {
            InitializeComponent();
        }

        private void mnuOpenLocalCamera_Click(object sender, EventArgs e)
        {
            FrmDevice x = new FrmDevice();
            if (x.ShowDialog(this) == DialogResult.OK)
            {
                // Create video source
                VideoCaptureDevice videoSource = new VideoCaptureDevice(x.VideoDevice);

                // Open it
                OpenVideoSource(videoSource);
            }
        }

        private void mnuOpenVideo_Click(object sender, EventArgs e)
        {
            OpenFileDialog x = new OpenFileDialog();
            if (x.ShowDialog(this) == DialogResult.OK)
            {
                // Create video source
                FileVideoSource videoSource = new FileVideoSource(x.FileName);

                // Open it
                OpenVideoSource(videoSource);
            }
        }

        /// <summary>
        /// Open video source
        /// </summary>
        /// <param name="source">video source</param>
        private void OpenVideoSource(IVideoSource source)
        {
            Cursor = Cursors.WaitCursor; // set busy cursor

            // Close previous video source
            vspMain.SignalToStop();
            vspMain.WaitForStop();

            // Start new video source
            vspMain.VideoSource = source;
            vspMain.Start();

            statIndex = statReady = 0; // reset statistics
            tmrMain.Start(); // start timer

            Cursor = Cursors.Default;
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            tmrMain.Enabled = false; // disable timer for get picture for progress

            vspMain.SignalToStop();
            vspMain.WaitForStop();
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void tmrMain_Tick(object sender, EventArgs e)
        {
            //hisMain.Values = face.FaceDetect(this, picFirst, picResult, txtOne, picFace, txtTwo);
            Bitmap face = null;
            picResult.Image = FaceDetect.FaceDetection.FaceDetect(current, ref face);
            picFace.Image = face;
        }

        private void hisMain_PositionChanged(object sender, AForge.Controls.HistogramEventArgs e)
        {
            if (e.Position != -1)
            {
                lblLevel.Text = String.Format("Level : {0}", e.Position);
            }
            else
            {
                lblLevel.Text = "Level : ";
            }
        }

        private void hisMain_SelectionChanged(object sender, AForge.Controls.HistogramEventArgs e)
        {
            lblLevel.Text = String.Format("Level : {0}..{1}", e.Min, e.Max);
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            //cam = new Camera();
            //cam.Init_Cam(this, picFirst.Width, picFirst.Height, picFirst); // initialize camera
            //cam.Start(); // start capture from camera
            tmrMain.Enabled = true; // enable timer for get picture for progress
            //face = new FaceDetection();
        }

        private void vspMain_NewFrame(object sender, ref Bitmap image)
        {
            //picResult.Image = FaceDetect.FaceDetection.FaceDetect(image);
            current = new Bitmap(image);
        }
    }
}